﻿	  	   							
package com.module.device.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.Transaction;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.manager.GenericHibernateManager;
import base.util.Page;

import com.module.device.dao.DeviceTypeDao;
import com.module.device.vo.DeviceType;

public class DeviceTypeManager extends GenericHibernateManager<DeviceType,Long> {

	private DeviceTypeManager(){}
	
	private static DeviceTypeManager manager;
	
	public synchronized static DeviceTypeManager getInstance(){
		if(manager == null)
			manager = new DeviceTypeManager();
		return manager;
	}
	
	private DeviceTypeDao dao = DeviceTypeDao.getInstance();
	
	/**
	 * 查询列表
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public Page list(Page pageObj,DeviceType pu) throws Exception{
		Session session = this.getSession();
		Page page = new Page();
		try {
			page = dao.getList(session,pu, pageObj.getStart(),pageObj.getLimit());
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return page;
	}
	
	
	/**
	 * 查询设备类型列表
	 * @param pageObj
	 * @param pu
	 * @return
	 * @throws Exception
	 */
	public DeviceType findByDeviceTypeName(String deviceTypeName) throws Exception{
		Session session = this.getSession();
		List list = new ArrayList();
		DeviceType dt = new DeviceType();
		try {
			 list = dao.findByDeviceTypeName(session,deviceTypeName);
			 if(list!=null&&!list.isEmpty())
			 {
				 dt = (DeviceType) list.get(0); 
			 }			 
		} catch (Exception e) {
			throw e;
		} finally {
			this.closeSession();
		}
		return dt;
	}
	
	
	
	
	
	
	
	
	
	
	/**
	 * 新增/修改
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	 public Map<String,Object> add(DeviceType obj) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	boolean flag = false;
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	try{
	    			    		
	    		 if(null!=obj.getId()&&0!=obj.getId()){//修改

	            	                 flag = dao.update(session, obj);

	             }else{

	                 flag = dao.save(session, obj);
	             }
	    		tx.commit();
	    	}catch(Exception e){
	    		tx.rollback();
	    		throw e;	    		
	    	}finally{
	    		this.closeSession();
	    	}
			map = optHandle(flag);
	    	return map;
	    }
	/**
	 * 删除任务
	 * @param obj
	 * @return
	 * @throws Exception
	 */
	  public Map<String,Object> delete(Long id) throws Exception
	    {
	    	Session session = this.getSession();
	    	Transaction tx =session.beginTransaction();
	    	Map<String,Object> map = new HashMap<String,Object>();
	    	boolean flag = false;
	    	try{
	    		DeviceType obj = dao.get(session, id);

	    		flag = dao.update(session, obj);
	            tx.commit();
	    	}catch(Exception e){
	            tx.rollback();
	            throw e;
	            
	    	}finally{
	    		this.closeSession();
	    	}
	    	map = optHandle(flag);
	    	return map;

	    }

	  /**
	   * 根据id查找
	   * @param id
	   * @return
	   * @throws Exception
	   */
	   public DeviceType findById(Long id) throws Exception{
		   Session session = this.getSession();
		   DeviceType vo = new DeviceType();
		   try{
			   vo = dao.get(session, id);
		   }catch (Exception e) {
				throw e;
		   }finally{
			   this.closeSession();
		   }
		   return vo;
	   }
	/**
	 * 操作处理返回
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue){
		String status = null;
		status = (isTrue == true ? "操作成功！": "操作失败！");
		return  optHandle(isTrue,status);
	}
	/**
	 * 操作处理返回
	 * @param isTrue
	 * @return
	 */
	public static Map<String, Object> optHandle(boolean isTrue,String message){
		Map<String, Object> statusMap = new HashMap<String, Object>();
		statusMap.put("state", isTrue);	
		statusMap.put("msg", message);
		return  statusMap;
	}
	@Override
	public GenericHibernateDao<DeviceType, Long> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
}
